<template>
  <view class="container">
		<nav-ber :title="'申请任务'" :bgColor="'#f7f8ff'"></nav-ber>
		<view class="province">
			<uFrom ref="getFrom" :itemList="itemList" :itemKeys="itemKeys" @setdata="setdataFun"></uFrom>
			<view class="button_str" @click="confirm">
				确认
			</view>
		</view>
  </view>
</template>
<script>
	import { applyById, timed_task_type, setApply, taskapplyById, upApplyTask } from '@/api/task.js'
	import { getbyuser, medicalRepresentative } from "@/api/login/index.js"
  export default {
    data() {
			return {
				rules: {
					name: [
						{
							required: true,
							message: "请输入姓名",
							// 可以单个或者同时写两个触发验证方式
							trigger: ["change", "blur"],
						},
						// {
						// 	// 自定义验证函数，见上说明
						// 	validator: (rule, value, callback) => {
						// 		// 上面有说，返回true表示校验通过，返回false表示不通过
						// 		// this.$u.test.mobile()就是返回true或者false的
						// 		return this.$u.test.chinese(value);
						// 	},
						// 	message: "姓名输入不正确",
						// 	// 触发器可以同时用blur和change
						// 	trigger: ["change", "blur"],
						// },
					],
				},
				keyword: '',
				ispro: '1',
				list: ['进行中', '已完成'],
				current: 0,
				itemList1: [
					{
						label: '任务名称',
						model: 'name',
						required: false,
						disabled: true,
						message: '请输入任务名称',
						type: 'input'
					},
					{
						label: '任务类型',
						model: 'typeName',
						modelName: 'type',
						required: false,
						disabled: true,
						message: '请输入任务类型',
						type: 'select',
						controls: 'select',
						keyName: 'dictLabel',
						url: 'system/dict/data/type/timed_task_type',
						value: 'dictValue',
						actions: [],
					},
					{
						label: '品种',
						model: 'drugName',
						disabled: true,
						required: false,
						message: '请输入品种',
						type: 'input'
					},
					{
						label: '省份',
						model: 'province',
						disabled: true,
						required: false,
						message: '请输入省份',
						type: 'input'
					},
					{
						label: '医院名称',
						model: 'hospital',
						modelName: 'hospitalList',
						required: true,
						disabled: false,
						message: '请选择医院名称',
						type: 'select',
						controls: 'select',
						keyName: 'name',
						url: 'base/hospital/list',
						value: 'id',
						dataType: 'rows',
						filter: false,
						provinceFil: true,
						actions: [],
					},
					{
						label: '申请数量',
						model: 'applyQty',
						required: true,
						message: '请输入申请数量',
						type: 'input',
						test: 'digits',
						maxlength: 10,
					},
					{
						label: '开始时间',
						model: 'startTime',
						disabled: true,
						required: false,
						message: '请选择开始时间',
						type: 'select',
						controls: 'time',
						title: '请选择时间',
					},
					{
						label: '截止时间',
						model: 'endTime',
						disabled: true,
						required: false,
						message: '请选择截止时间',
						type: 'select',
						controls: 'time',
						title: '请选择时间',
					},
					{
						label: '申请人',
						model: 'createBy',
						disabled: true,
						required: false,
						message: '请输入申请人',
						type: 'input'
					},
					// {
					// 	label: '创建时间',
					// 	model: 'createTime',
					// 	disabled: true,
					// 	required: false,
					// 	message: '请选择创建时间',
					// 	type: 'select',
					// 	controls: 'time',
					// 	title: '请选择时间',
					// },
				],
				itemList: [],
				itemList2: [
					{
						label: '任务名称',
						model: 'name',
						required: false,
						disabled: true,
						message: '请输入任务名称',
						type: 'input'
					},
					{
						label: '任务类型',
						model: 'typeName',
						modelName: 'type',
						required: false,
						disabled: true,
						message: '请输入任务类型',
						type: 'select',
						controls: 'select',
						keyName: 'dictLabel',
						url: 'system/dict/data/type/timed_task_type',
						value: 'dictValue',
						actions: [],
					},
					{
						label: '品种',
						model: 'drugName',
						disabled: true,
						required: false,
						message: '请输入品种',
						type: 'input'
					},
					{
						label: '省份',
						model: 'province',
						disabled: true,
						required: false,
						message: '请输入省份',
						type: 'input'
					},
					{
						label: '医院名称',
						model: 'hospital',
						modelName: 'hospitalList',
						url: 'base/hospital/list',
						mulData: 'mulData',
						required: true,
						message: '请输入医院名称',
						type: 'mulSelect',
						filter: false,
						provinceFil: true,
					},
					{
						label: '申请数量',
						model: 'applyQty',
						required: true,
						message: '请输入申请数量',
						type: 'input',
						test: 'digits',
						maxlength: 10,
					},
					{
						label: '开始时间',
						model: 'startTime',
						disabled: true,
						required: false,
						message: '请选择开始时间',
						type: 'select',
						controls: 'time',
						title: '请选择时间',
					},
					{
						label: '截止时间',
						model: 'endTime',
						disabled: true,
						required: false,
						message: '请选择截止时间',
						type: 'select',
						controls: 'time',
						title: '请选择时间',
					},
					{
						label: '申请人',
						model: 'createBy',
						disabled: true,
						required: false,
						message: '请输入申请人',
						type: 'input'
					},
					// {
					// 	label: '创建时间',
					// 	model: 'createTime',
					// 	disabled: true,
					// 	required: false,
					// 	message: '请选择创建时间',
					// 	type: 'select',
					// 	controls: 'time',
					// 	title: '请选择时间',
					// },
				],
				itemKeys: {
					titles: '',
					sel: ''
				}
			}
    },
		methods: {
			taskapplyByIdFun(id) {
				taskapplyById(id).then((res) => {
					let {name, type, drugName, province, startTime, endTime, createTime  } = res.data
					timed_task_type().then((item) => {
						this.itemKeys = {
							...res.data,
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
						}
						if (type == 4) {
							this.itemList = JSON.parse(JSON.stringify(this.itemList2)) 
						} else {
							this.itemList = JSON.parse(JSON.stringify(this.itemList1))
						}
					})
					
				})
			},
			applyByIdFun(id) {
				applyById(id).then((res) => {
					let {name, type, drugName, province, startTime, endTime, createTime  } = res.data
					timed_task_type().then((item) => {
						this.itemKeys = {
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
							hospital: '',
							applyQty: '',
							hallId: res.data.id,
							status: 0,
							createBy: JSON.parse(uni.getStorageSync('userInfo')).userName,
							name,
							type,
							drugName,
							province,
							startTime,
							endTime,
							createTime
						}
						if (type == 4) {
							this.itemList = JSON.parse(JSON.stringify(this.itemList2)) 
						} else {
							this.itemList = JSON.parse(JSON.stringify(this.itemList1))
						}
					})
					
				})
			},
			setdataFun(val) {
				(this.type? upApplyTask : setApply)({
					...val,
					applyQty: +val.applyQty,
					status: 0,
					hospitalId: val.hospitalList[0].hospitalId,
					hospitalList: val.hospitalList
				}).then((res) => {
					uni.showToast({
					  title: `${this.type? '修改':'申请'}成功`,
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								uni.navigateBack({
									delta: 1,
								})
							}, 500)
						}
					})
				})
			},
			confirm() {
				// console.log()
				// console.log('this.$refs.getFrom', this.$refs.getFrom.submit())
				this.$refs.getFrom.submit()
			},
			proFun(val) {
				this.ispro = val
			},
			sectionChange(index) {
				this.current = index;
			},
		},
		onShow() {
			((JSON.parse(uni.getStorageSync('userInfo')).userType == '02')? medicalRepresentative : getbyuser)(JSON.parse(uni.getStorageSync('userInfo')).userId).then((resmin) => {
				uni.setStorage({
					key: 'userInfoRole',
					data: JSON.stringify(resmin.data)
				})
			})
		},
		onLoad(option) {
			if (option.type) {
				this.type = true
				this.taskapplyByIdFun(option.id)
			} else {
				this.type = false
				this.applyByIdFun(option.id)
			}
			
		},
		mounted() {
			
			
		},
  }
</script>
<style lang="scss" scoped>
	.sea_pad {
		padding: 0 30rpx;
		box-sizing: border-box;
	}
	.container {
		background-color: #f7f8ff;
		// background-color: #fff;
		display: flex;
		flex-direction: column;
		height: 100vh;
		overflow: hidden;
	}
	.province {
		flex: 1;
		overflow: scroll;
		padding: 0 20rpx;
		margin-top: 20rpx;
		padding: 0 40rpx;
		box-sizing: border-box;
	}
	.button_str {
		width: 80%;
		margin: auto;
		text-align: center;
		border-radius: 100rpx;
		padding: 20rpx 10rpx;
		background-color: rgb(3, 121, 255);
		color: #fff;
		box-sizing: border-box;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}
</style>
